from common.request import JsonResponse, _Root
from dota.models import DotaFunctioneer, Video
import datetime

class _Video(_Root):

    def get(self):
        functioneer = self.GET.get('functioneer', '')
        page_size = int(self.GET.get('pageSize', 10))
        current_page = int(self.GET.get('currentPage', 1))
        params = {}
        exp = '''SELECT 
                    DF.name,
                    V.title,
                    V.cover_address,
                    V.duration,
                    V.video_address,
                    V.video_flash_address,
                    V.src_video_id,
                    V.src_video_id_encryption,
                    V.publish_date
                FROM dota_functioneer DF INNER JOIN  video V ON DF.id = V.user_id
                where 1=1 '''

        if functioneer != '':
            exp += ''' and DF.id=:functioneer'''
            params['functioneer'] = functioneer
        exp += ''' order by v.publish_date desc limit {0} offset {1}'''.format(page_size, page_size * (current_page - 1))
        videos = self.db.execute(exp, params)
        data = []
        for video in videos:
            video_info = {}
            for key, value in video.items():
                if isinstance(value, datetime.date):
                    video_info[key] = value.strftime('%Y-%m-%d')
                else:
                    video_info[key] = value
            data.append(video_info)
        return JsonResponse(self, '50000', data=data)
